package com.taobao.ic.ticket.service.impl;

import org.springframework.beans.factory.annotation.Autowired;

import com.taobao.ic.ticket.domain.UserDO;
import com.taobao.ic.ticket.exception.BizException;
import com.taobao.ic.ticket.exception.ErrorCodeException;
import com.taobao.ic.ticket.exception.Exceptions;
import com.taobao.ic.ticket.manager.UserManager;
import com.taobao.ic.ticket.result.ResultDO;
import com.taobao.ic.ticket.service.UserService;

public class UserServiceImpl implements UserService {

	// private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private UserManager userManager;

	@Override
	public ResultDO<UserDO> getUserById(long userId) throws BizException {
		final ResultDO<UserDO> result = new ResultDO<UserDO>();
		final UserDO user;
		try {
			
			
			
			
			user = userManager.getUserById(userId);
			result.setModel(user);
			
		} catch (ErrorCodeException e) {
			Exceptions.injectErrorCode(result, e);
		}
		return result;
	}

	@Override
	public ResultDO<UserDO> login(String username, String password)
			throws BizException {	
		final ResultDO<UserDO> result = new ResultDO<UserDO>();
		final UserDO user;
		try {
			user = userManager.login(username, password);
			result.setModel(user);
			if(user!=null)result.setSuccess(true);
		    } catch (ErrorCodeException e) {
			Exceptions.injectErrorCode(result, e);
		}
		return result;
	}

	@Override
	public ResultDO<UserDO> regist(UserDO userDo) throws BizException {
		final ResultDO<UserDO> result=new ResultDO<UserDO>();
		final UserDO user;
		try{
			
			
			user=userManager.register(userDo);
			result.setModel(user);
			
		}catch (ErrorCodeException e) {
			Exceptions.injectErrorCode(result, e);
		}
		return result;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

}
